package xy.donate.android.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import xy.donate.android.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 含有消息提示数的带有图片的item
 */
public class ImageWithCountView extends LinearLayout {
    @BindView(R.id.view_text)
    TextView viewText;
    @BindView(R.id.view_count)
    TextView count;
    @BindView(R.id.view_image)
    ImageView image;

    public ImageWithCountView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ImageWithCountView(Context context) {
        super(context);
        init(context, null);
    }

    public ImageWithCountView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_image_with_count, this);
        ButterKnife.bind(view);
        TypedArray tr = context.obtainStyledAttributes(attrs, R.styleable.ImageWithCountView);
        int srcId = tr.getResourceId(R.styleable.ImageWithCountView_viewSrc, -1);
        if (srcId != -1) {
            image.setImageResource(srcId);
        }
        String text = tr.getString(R.styleable.ImageWithCountView_viewText);
        if (!TextUtils.isEmpty(text)) {
            viewText.setText(text);
        }
        setGravity(Gravity.CENTER_HORIZONTAL);
    }

    public TextView getViewText() {
        return viewText;
    }

    public TextView getCountText() {
        return count;
    }

    public void setCount(int num) {
        if (num > 0) {
            count.setVisibility(VISIBLE);
        } else {
            count.setVisibility(GONE);
        }
        count.setText(num + "");
    }

    public ImageView getImageView() {
        return image;
    }
}
